package db;

import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.Map;

public class DBConfigLoader {
    private static final String LOCAL_CONFIG_FILE = "/db_config_local.yml";
    private static final String DEFAULT_CONFIG_FILE = "/db_config.yml";
    private static final Map<String, Map<String,String>> config;

    static {
        Yaml yaml = new Yaml();
        try (InputStream in = DBConfigLoader.class.getResourceAsStream(LOCAL_CONFIG_FILE)) {
            if (in != null) {
                config = yaml.loadAs(in, Map.class);
            } else {
                try (InputStream defaultIn = DBConfigLoader.class.getResourceAsStream(DEFAULT_CONFIG_FILE)) {
                    config = yaml.loadAs(defaultIn, Map.class);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to load configuration file", e);
        }
    }
    public static String getUrl() {
        return config.get("database").get("url");
    }

    public static String getUser() {
        return config.get("database").get("user");
    }

    public static String getPassword() {
        return config.get("database").get("password");
    }

    public static void main(String[] args) {
        System.out.println(getUrl());
        System.out.println(getUser());
        System.out.println(getPassword());
    }
}